package clientcore;

import java.util.HashMap;
import java.util.List;
import java.util.StringTokenizer;
import java.util.Vector;

import android.util.Log;

import clientresponse.ResponseBoardQuestion;
import clientresponse.ResponseBuddies;
import clientresponse.ResponseBuddyAccept;
import clientresponse.ResponseBuddyInvite;
import clientresponse.ResponseBuddyRemove;
import clientresponse.ResponseChatPrivate;
import clientresponse.ResponseQuestionLog;
import clientresponse.ResponseUpdateBuddyInvites;
import clientresponse.ResponseUpdateChatPrivate;

/** GameState
 * @brief An auxiliary class for storing and updating in-game data such as
 * player's position, avatar, online buddies, etc.
 * @author brian
 */
public class GameState {
	public static final String TAG = GameState.class.getSimpleName();
    public static AndroidMain am = null;
    private static short loginStatus = 1;
    private static boolean isConnected = false;
    private static String characterName;
    private static String userName;
    private static boolean hasQuestion = false;
    private static HashMap<String, Boolean> buddies = null;
    private static List<Question> questionLog = null;
    private static Vector<Message> messageList = new Vector<Message>();
    private static Vector<Message> inviteList = new Vector<Message>();
    
/* STATE METHODS */    
    public static boolean hasQuestion() { return hasQuestion; }
    public static void clearQuestion() { hasQuestion = false; } 
    public static void setUserName(String name) { userName = name; }
    public static String getUserName() { return userName; }
    
    public static short login(String username, String password) {
        am.addArguments(Constants.CLIENT_VERSION);
        am.addArguments(username);
        am.addArguments(password);
        am.sendRequest(Constants.CMSG_AUTH);
        return loginStatus = am.handleResponse();
    }
    public static short logout() {
    	/* TODO Implement
  	  am.sendRequest(Constants.CMSG_DISCONNECT);  
      return loginStatus = am.handleResponse();

      */
        return 1;
       // wipe game state? merge this with disconnect() ?
    
    }
    public static void disconnect() {
    	isConnected = false;
    	am.closeServer();
    	am = null;
    }
    public static boolean isConnected() { return isConnected; }

/* CHAT METHODS */
    /**
     * sendPrivateChat()
     * @brief sends a chat message to a character in the game.
     * @param charName
     * @param message
     * @return status -1 does not exist | 0 offline | 1 success
     */
    public static short sendPrivateChat(String charName, String message) {
    	am.addArguments(charName);
    	am.addArguments(message);
    	am.sendRequest(Constants.CMSG_PRIVATE_CHAT);
    	am.handleResponse();
    	return ResponseChatPrivate.status;
    }
    public static Vector<Message> updatePrivateChats()
    {
    	am.sendRequest(Constants.CMSG_GET_PRIVATE_CHAT);
    	am.handleResponse();
    	return ResponseUpdateChatPrivate.privateMessages;
    }
/* BUDDY METHODS */
    public static Vector<Message> updateBuddyInvites()
    {
    	am.sendRequest(Constants.CMSG_GET_BUDDY_INVITES);
    	am.handleResponse();
    	return ResponseUpdateBuddyInvites.buddyInvites;
    }
    /**
     * inviteBuddy()
     * @brief sends a friend invite to a character in the game.
     * @param buddyName
     * @param message
     * @return status -1 does not exist | 0 offline | 1 success
     */
    public static short inviteBuddy(String buddyName, String message) {
    	am.addArguments(buddyName);
    	am.addArguments(message);
    	am.sendRequest(Constants.CMSG_BUDDY_INVITE);
    	am.handleResponse();
    	return ResponseBuddyInvite.status;
    }
    public static short inviteBuddy(String buddyName) {
    	return inviteBuddy(buddyName, 
    			"Please add me to your friends list.");
    }
    /**
     * respondToInvite()
     * @param buddyName
     * @param accept   0 --> no, 1 --> yes
     * @return status 0-->error 1-->success
     */
    public static short respondToInvite(String buddyName, boolean accept) {
    	am.addArguments(buddyName);
    	am.addArguments((short) (accept? 1:0));
    	am.sendRequest(Constants.CMSG_BUDDY_ACCEPT);
    	am.handleResponse();
    	return ResponseBuddyAccept.status;
    }
    
    public static short removeBuddy(String buddyName) {
    	am.addArguments(buddyName);
    	am.sendRequest(Constants.CMSG_BUDDY_REMOVE);
    	am.handleResponse();
    	return ResponseBuddyRemove.status;
    }
    public static HashMap<String, Boolean> getBuddies() {
    	// Sends request for buddies string. Buddies will be in the format:
    	// "name online/offline,[ name online/offline,[ ... ,]"
    	// where online/offline is 0 or 1.
    	am.sendRequest(Constants.CMSG_BUDDIES);
    	am.handleResponse();
    	buddies = new HashMap<String, Boolean>();
    	if(!(ResponseBuddies.buddies.equals("")) && !(ResponseBuddies.buddies == null))
    	{
        	//buddies = new HashMap<String, Boolean>();
    		StringTokenizer st = new StringTokenizer(ResponseBuddies.buddies,",");
	    	while(st.hasMoreTokens()) {
	    		String[] buddy = st.nextToken().split(" ");
	    		buddies.put(buddy[0], new Boolean(buddy[1]));
	    	}
    	}
    	return buddies;
    }
    
/* QUESTION METHODS */
    public static void answerQuestion() {
    	hasQuestion = false;
        am.addArguments(ResponseBoardQuestion.question.getID());
        am.addArguments(ResponseBoardQuestion.question.getChoice());
        am.sendRequest(Constants.CMSG_BOARD_ANSWER);
        am.handleResponse();
    }
    public static void updateQuestion() {
    	hasQuestion = true;
        am.sendRequest(Constants.CMSG_BOARD_QUESTION);
        am.handleResponse();
    }
    public static void updateQuestionLog() {
    	questionLog = null;
    	am.sendRequest(Constants.CMSG_QUESTION_LOG);
    	am.handleResponse();
    	questionLog = ResponseQuestionLog.questionLog;
    }
    public static List<Question> getQuestionLog() {
    	return questionLog;
    }
    public static Vector<Message> getMessageList() {
    	return messageList;
    }
    public static void addMessage(Message chat) {
    	messageList.add(chat);
    }
    public static void removeMessage(int position) {
    	messageList.remove(position);
    }
    public static Vector<Message> getInviteList() {
    	return inviteList;
    }
    public static void addInvite(Message invite) {
    	inviteList.add(invite);
    }
    public static void removeInvite(int position) {
    	inviteList.remove(position);
    }
    private GameState() {
        am = new AndroidMain();
        isConnected = am.connectServer();
    }
    public static void initialize() {
		new GameState();
    }
}
